package com.mxpio.erp.equipment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mxpio.erp.common.equipment.entity.*;
import com.mxpio.erp.common.equipment.service.*;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 点检计划明细Service业务层处理
 *
 * @author Cheng
 * @date 2023-07-19
 */
@Service
public class EquipmentUpkeepPlanDetailServiceImpl extends BaseServiceImpl<EquipmentUpkeepPlanDetail> implements EquipmentUpkeepPlanDetailService {

    @Resource
    private EquipmentUpkeepDetailService equipmentUpkeepDetailService;
    @Resource
    private EquipmentUpkeepDetailParamService equipmentUpkeepDetailParamService;
    @Resource
    private EquipmentUpkeepPlanDetailParamService equipmentUpkeepPlanDetailParamService;
    @Resource
    private EquipmentCommParametersDetailService equipmentCommParametersDetailService;
    @Override
    @Transactional
    public void addDetailByCategory(String planId, String categoryId) {
        // 查询分类下的明细，然后保存
        List<EquipmentUpkeepDetail> tpmUpkeepDetails = equipmentUpkeepDetailService.selectByMainId(categoryId);
        tpmUpkeepDetails.forEach(categoryDetail -> {
            EquipmentUpkeepPlanDetail planDetail = new EquipmentUpkeepPlanDetail();
            BeanUtil.copyProperties(categoryDetail,planDetail,"id");
            planDetail.setLowerLimit(categoryDetail.getLowerLimit());
            planDetail.setUpperLimit(categoryDetail.getUpperLimit());
            planDetail.setDetailId(categoryDetail.getId());
            planDetail.setPlanId(planId);
            save(planDetail);

            // 处理计划明细中的选项数据
            if (planDetail.getChooseEnable() == 1) {
                List<EquipmentUpkeepDetailParam> tpmUpkeepDetailParams = equipmentUpkeepDetailParamService.selectByMainId(categoryDetail.getId());
                List<EquipmentUpkeepPlanDetailParam> upkeepPlanDetailParams = tpmUpkeepDetailParams.stream().map(categoryParam -> {
                    EquipmentUpkeepPlanDetailParam planDetailParam = new EquipmentUpkeepPlanDetailParam();
                    BeanUtil.copyProperties(categoryParam, planDetailParam,"id","createTime","createBy","updateBy","updateTime","createDept");
                    planDetailParam.setDetailId(planDetail.getId());
                    return planDetailParam;
                }).collect(Collectors.toList());
                equipmentUpkeepPlanDetailParamService.save(upkeepPlanDetailParams);
            }
        });
    }

    @Override
    @Transactional
    public void addDetail(EquipmentUpkeepPlanDetail equipmentUpkeepPlanDetail) {
        save(equipmentUpkeepPlanDetail);
        // 处理计划明细中的选项数据
        if (equipmentUpkeepPlanDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> tpmUpkeepDetailParams = equipmentCommParametersDetailService.selectByMainId(equipmentUpkeepPlanDetail.getParamId());
            List<EquipmentUpkeepPlanDetailParam> upkeepPlanDetailParams = tpmUpkeepDetailParams.stream().map(categoryParam -> {
                EquipmentUpkeepPlanDetailParam planDetailParam = new EquipmentUpkeepPlanDetailParam();
                BeanUtil.copyProperties(categoryParam, planDetailParam,"id","createTime","createBy","updateBy","updateTime","createDept");
                planDetailParam.setDetailId(equipmentUpkeepPlanDetail.getId());
                return planDetailParam;
            }).collect(Collectors.toList());
            equipmentUpkeepPlanDetailParamService.save(upkeepPlanDetailParams);
        }
    }

    @Override
    @Transactional
    public List<EquipmentUpkeepPlanDetail> selectByMainId(String mainId) {
        Criteria c = Criteria.create().addCriterion("planId", Operator.EQ, mainId);
        return list(EquipmentUpkeepPlanDetail.class,c);
    }
}
